%{?!tool_name: %global tool_name hosttools}
%global tool_prefix /opt/%{tool_name}

Name: %{tool_name}-llvm
Version: 14.0.1
Release: 1%{?dist}
Summary: LLVM compiler infrastructure

License: GPLv3+
URL: https://llvm.org
Source0: https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.1/llvm-project-%{version}.src.tar.xz
Patch0: llvm-sanitizer-link-libgcc.patch
Patch1: clang-link-libgcc.patch

BuildRequires: gcc-c++ cmake ninja python3

%description
LLVM compiler infrastructure and Clang tools, lld linker

%package devel
Summary: LLVM, Clang and lld development files

%description devel
This package contains LLVM, Clang, lld static libs and headers.

%prep
%setup -q -n llvm-project-%{version}.src
%patch0 -p1
%patch1 -p1

%build
mkdir build-llvm
cd build-llvm
cmake -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=%{tool_prefix} \
  -DLLVM_ENABLE_PROJECTS='clang;compiler-rt;lld' \
  -DLLVM_TARGETS_TO_BUILD=X86 \
  -DLLVM_INCLUDE_TESTS=OFF \
  -DLLVM_INCLUDE_EXAMPLES=OFF \
  -DLLVM_INCLUDE_BENCHMARKS=OFF \
  -DCOMPILER_RT_INCLUDE_TESTS=OFF \
  -DCLANG_DEFAULT_RTLIB=compiler-rt \
  -DLLVM_LIBDIR_SUFFIX=/llvm \
  ../llvm
ninja

%install
cd build-llvm
DESTDIR=%{buildroot} ninja install/strip

%files
%{tool_prefix}/bin/*
%exclude %{tool_prefix}/bin/llvm-config
%{tool_prefix}/lib/llvm/clang/%{version}
%{tool_prefix}/libexec/*
%{tool_prefix}/share/clang
%{tool_prefix}/share/opt-viewer
%{tool_prefix}/share/scan-build
%{tool_prefix}/share/scan-view
%{tool_prefix}/share/man/man1/*
# package libclang.so for c-index-test
%{tool_prefix}/lib/llvm/libclang.so
%{tool_prefix}/lib/llvm/libclang.so.13
%{tool_prefix}/lib/llvm/libclang.so.%{version}

%files devel
%{tool_prefix}/bin/llvm-config
%{tool_prefix}/include/clang-c
%{tool_prefix}/include/clang
%{tool_prefix}/include/lld
%{tool_prefix}/include/llvm-c
%{tool_prefix}/include/llvm
%{tool_prefix}/lib/libear
%{tool_prefix}/lib/libscanbuild
%{tool_prefix}/lib/llvm
%exclude %{tool_prefix}/lib/llvm/clang/%{version}
%exclude %{tool_prefix}/lib/llvm/libclang.so
%exclude %{tool_prefix}/lib/llvm/libclang.so.13
%exclude %{tool_prefix}/lib/llvm/libclang.so.%{version}

%changelog

